<?php
class Application_Model_DbTable_Mail extends Zend_Db_Table_Abstract{
	protected $_name = 'mail';
	
	public function fetchAllWithAuthor(){
		$select = $this->select();
		$select->setIntegrityCheck(false);
		
		$select	->from(array('n'=>$this->_name))
				->joinLeft(array('p'=>'personne'), 'n.author=p.id', array('nom','prenom'))
				->order(array('read ASC','date DESC'));	
		return $select->query()->fetchAll(PDO::FETCH_OBJ);
	}
	public function findWithAuthor($id){
		$select = $this->select();
		$select->setIntegrityCheck(false);
		$select	->from(array('n'=>$this->_name))
				->joinLeft(array('p'=>'personne'), 'n.author=p.id', array('nom','prenom'))
				->where('n.id = ?',$id);
		return $select->query()->fetch(PDO::FETCH_OBJ);
	}
	public function countUnread(){
		$select = $this->select();
		$select ->from($this->_name,'COUNT(*) as count')
				->where($this->_name.'.read = ?', new Zend_Db_Expr('FALSE'));
		return $select->query()->fetch(PDO::FETCH_OBJ);
	}
}